<?php

namespace app\api\controller\supplier;

use app\api\base\RestfulBase;
use app\modules\supplier\logic\ShopGoodsLogic;

Class ShopGoods extends RestfulBase
{
    protected $logic;

    protected function _initialize()
    {

        $this->logic = new ShopGoodsLogic();

    }

    /**
     * 获取商品列表all
     * @return Response
     */
    public function index(){
       return  $this->_indexQuery('all');
    }

    /**
     * 获取商品列表my
     * @return Response
     */
    public function my_index(){
      return  $this->_indexQuery('my');
    }

    /**
     * 获取商品列表
     * @return Response
     */
    protected function _indexQuery($type = null)
    {
        if ($this->request->isGet()) {

            if (empty($type)){
                return $this->apiError('请求类型不能为空！');
            }
            $option = [
                'is_pages' => ($this->getParams('is_pages', 1, 'intval') >= 1 ? true : false),//是否分页查询
                'page_current' => $this->getParams('page_current', 1, 'intval'),//分页查询时，当前页码
                'page_size' => $this->getParams('page_size', 10, 'intval'),
                'order' => $this->getParams('order', 'DESC', 'trim,strip_tags,strtoupper'),
                'sort' => $this->getParams('sort', '', 'trim,strip_tags'),//排序字段
                'cate_id' => $this->getParams('cate_id', 0, 'intval'),//分类ID
                'keyword' => $this->getParams('keyword', '', 'trim,strip_tags'),
            ];

            if ($option['page_current'] < 1) {
                $option['page_current'] = 1;
            }

            if ($type == 'all'){ //所有商品列表
                $data = $this->logic->index($option);
            }elseif($type == 'my'){
                $data = $this->logic->myIndex($this->_account['supplier_id'],$option);
            }else{
                return $this->apiError('无效请求类型');
            }

            if (is_array($data)) {
                return $this->apiSuccess($data);
            } else {
                return $this->apiError($this->logic->getError());
            }
        } else {
            return $this->apiError('无效请求');
        }
    }

    /**
     * 获取商品详情 all
     * @return Response
     */
    public function read(){
        return $this->_readQuery('all');
    }

    /**
     * 获取商品详情 my
     * @return Response
     */
    public function my_read(){
        return $this->_readQuery('my');
    }

    /**
     * 获取商品详情
     * @return Response
     */
    public function _readQuery($type = null)
    {
        if ($this->request->isGet()){
           $goods_id = $this->getParams('goods_id','','trim,strip_tags');
            if (empty($type)){
                return $this->apiError('请求类型不能为空！');
            }

            if ($type == 'all'){
                $data = $this->logic->getGoods($goods_id);
            }elseif($type == 'my'){
                $data = $this->logic->getMyGoods($this->_account['supplier_id'],$goods_id);
            }else{
                $this->apiError('无效请求类型！');
            }

           if (is_array($data)){
               return $this->apiSuccess($data);
           }else{
               return $this->apiError($this->logic->getError());
           }
        }else{
            return $this->apiError('无效请求');
        }
    }

    /**
     * 上架供应商商品数据
     * @return Response
     */
    public function create(){
         if($this->request->isPost()){
               $data['supplier_id'] = $this->_account['supplier_id'];
               $data['data']= $this->getParams('data/a');
//               dump($data);exit();
               if ($this->logic->addGoodsSpes($data)){
                   return $this->apiSuccess();
               }else{
                   return $this->apiError($this->logic->getError());
               }
         }else{
             return $this->apiError('无效请求');
         }
    }

    /**
     * 上架供应商商品数据
     * @return Response
     */
    public function update(){
        if($this->request->isPost()){
            $data= $this->getParams('data/a');
            if ($this->logic->editGoodsSpes($data)){
                return $this->apiSuccess();
            }else{
                return $this->apiError($this->logic->getError());
            }
        }else{
            return $this->apiError('无效请求');
        }
    }


    /**
     * 删除供应商规格 all
     * @return Response
     */
    public function delete_all(){
        if($this->request->isPost()){
            $data['supplier_id'] = $this->_account['supplier_id'];
            $data['goods_id'] = $this->getParams('goods_id','','trim,strip_tags');

            if ($this->logic->delGoodsSpes($data)){
                return $this->apiSuccess();
            }else{
                return $this->apiError($this->logic->getError());
            }
        }else{
            return $this->apiError('无效请求');
        }
    }

    /**
     * 删除供应商规格
     * @return Response one
     */
    public function delete_one(){
        if($this->request->isPost()){
            $data['supplier_id'] = $this->_account['supplier_id'];
            $data['specval_id'] = $this->getParams('specval_id','','trim,strip_tags');

            if ($this->logic->delGoodsSpesOne($data)){
                return $this->apiSuccess();
            }else{
                return $this->apiError($this->logic->getError());
            }
        }else{
            return $this->apiError('无效请求');
        }
    }


}

